import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        flag: 0,
        //数据源
        sourceList: [],
        validFlag: 0,
        allFields:[],
        dispatchMenu: [],
        imMenu:[],
        sqlList:[],
        sqlInfo:{},
        totleNum: 0,
        totleNum2: 0,
        typeList:[],
        connectInfo:{}
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg;
            state.flag = payload._flag;
        },
        add_source(state, payload) {
            state.sourceList = [].concat(payload.com_sourcelist);
        },
        change_flag(state, payload) {
            state.validFlag = payload.flag;
            state.msg = payload._msg;
        },
        add_field(state, payload) {
            state.allFields = payload.com_field
        },
        add_dispatch(state, payload) {
            state.dispatchMenu = [].concat(payload.com_dispatchmenu);
        },
        add_immenu(state, payload) {
            state.imMenu = [].concat(payload.com_immenu);
        },
        add_sqllist(state, payload) {
            state.sqlList = [].concat(payload.com_sql_list);
            state.totleNum2 = payload.com_total;
        },
        add_sqlinfo(state, payload) {
            state.sqlInfo = Object.assign({}, payload.com_sql_info);
        },
        add_typelist(state, payload) {
            state.typeList = [].concat(payload.com_typelist);
            state.totleNum = payload.com_total;
        },
        add_connect_info(state, payload) {
            state.connectInfo = Object.assign({},payload.com_connect_info);
        }
    },
    actions: {
        //获取数据源下拉菜单选项
        getSourceList({ commit }, payload) {
            return axios.post('/api/connect/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_source',{
                            com_sourcelist: res.data.data.t
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //验证sql
        verifySql({ commit }, payload) {
            return axios.post('/api/dashboard/validSql', payload)
                .then((res) => {
                    if(res.data.data.validFlag == "") {
                        commit('change_flag', {
                            flag: 1
                        })
                    }else{
                        commit('change_flag', {
                            flag: 0,
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取所有字段
        getAllFields({ commit }, payload) {
            return axios.post('/api/dashboard/loadFieldList', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_field',{
                            com_field: res.data.data
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取调度方式
        getDispatchMenu({ commit }, payload) {
            return axios.post('/api/schedule/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_dispatch', {
                            com_dispatchmenu: res.data.data.t,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取及时通讯
        getImMenu({ commit }, payload) {
            return axios.post('/api/im/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_immenu', {
                            com_immenu: res.data.data.t,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //创建dashboard
        createDashboard({ commit }, payload) {
            return axios.post('/api/dashboard/create', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: '新建SQL表盘成功',
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //获取sql列表
        getSqlList({ commit }, payload) {
            return axios.post('/api/dashboard/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_sqllist', {
                            com_sql_list: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //通过rowId获取sql详情
        getSqlInfo({ commit }, payload) {
            return axios.post('/api/dashboard/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_sqlinfo', {
                            com_sql_info: res.data.data
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //通过rowId删除sql
        delSql({ commit }, payload) {
            return axios.post('/api/dashboard/del', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "删除成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //更新sql
        updateSql({ commit }, payload) {
            return axios.post('/api/dashboard/update', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "更新成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //新建表盘类型
        createDashType({ commit }, payload) {
            return axios.post('/api/auth/type/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //表盘类型列表分页
        getTypeList({ commit }, payload) {
            return axios.post('/api/auth/type/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_typelist',{
                            com_typelist: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //表盘类型删除
        delType({ commit }, payload) {
            return axios.post('/api/auth/type/del',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //类型开启
        openType({ commit }, payload) {
            return axios.post('/api/auth/type/open',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "开启成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //部门关闭
        closeType({ commit }, payload) {
            return axios.post('/api/auth/type/close',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "关闭成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //单独保存内容描述
        contentUpdate({ commit }, payload) {
            return axios.post('/api/dashboard/contentUpdate',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "内容描述保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取数据库连接详情
        getConnectById({ commit }, payload) {
            return axios.post('/api/connect/info',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_connect_info',{
                            com_connect_info: res.data.data
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}